﻿#include "maininterface.h"
#include "ui_maininterface.h"
#include<QSqlDatabase>
#include<QPalette>
#include<QPixmap>
#include<QMessageBox>
#include<QHBoxLayout>
#include<QDebug>

MainInterface::MainInterface(QWidget *parent)
    : QMainWindow(parent, Qt::FramelessWindowHint)
    , ui(new Ui::MainInterface)
{
    ui->setupUi(this);
    initializeStyleSheet();

    //语音播报
    m_speech = new QTextToSpeech;

    //初始化托盘
    initTray();

    //关闭按钮
    connect(ui->closePushButton, &QAbstractButton::clicked,
            this, [&](){

        this->close();
    });

    connect(ui->minPushButton,  &QAbstractButton::clicked,
            this, [&](){
        this->hide();
        trayIcon->show();
        trayIcon->showMessage("通知", "软件已隐藏到菜单栏！", QSystemTrayIcon::Information, 700);//最后一个参数为提示时长，默认10000，即10s
    });


}

MainInterface::~MainInterface()
{
    delete m_speech;
    //关闭数据库
    QSqlDatabase db = QSqlDatabase::database("connection1");
    db.close();
    delete ui;
}

void MainInterface::iconActivated(QSystemTrayIcon::ActivationReason reason)
{
    switch (reason)
    {
    case QSystemTrayIcon::Trigger:
        trayIcon->showMessage(tr("深度学习图片识别系统"),
                                  tr("欢迎使用此程序！"),
                                  QSystemTrayIcon::Information,
                                  700);
        break;
    case QSystemTrayIcon::DoubleClick:
        //trayIcon->showMessage("title","你双击了");
        //setVisible(true);
        this->show();
        break;
//  case QSystemTrayIcon::MiddleClick://鼠标中间滚轮
//            trayIcon->showMessage("title","你中键了");
//            break;
    default:
        break;
    }
}

void MainInterface::onShowMainAction()
{
    this->show();
}

void MainInterface::onExitAppAction()
{
    this->close();
}

void MainInterface::initTrayIcon()
{
    //判断系统是否支持托盘图标显示
    if(!QSystemTrayIcon::isSystemTrayAvailable())
     {
            qDebug() <<"系统不支持托盘";
            return;
      }
    //实例化托盘图标控件
    trayIcon = new QSystemTrayIcon(this);
    trayIcon->setIcon(QIcon(":/Image/main_menu.png"));  //设置托盘图标显示
    trayIcon->setToolTip("Calculator"); //显示提示信息

    //创建托盘菜单
   trayShowMainAction = new QAction(tr("显示主界面"), this);
   trayExitAppAction = new QAction(tr("退出"), this);
    trayMenu = new QMenu(this);
    trayMenu ->addAction(trayShowMainAction);//添加事件
    trayMenu ->addSeparator();//添加分割线
    trayMenu ->addAction(trayExitAppAction);
    trayIcon->setContextMenu(trayMenu );//托盘添加菜单
}

void MainInterface::initializeStyleSheet()
{
    //关闭按钮
    ui->closePushButton->setStyleSheet("QPushButton#closePushButton{border-image: url(:/Image/close.png)}");
    //关闭按钮
    ui->minPushButton->setStyleSheet("QPushButton#minPushButton{border-image: url(:/Image/min.png)}");
//    //绘制背景颜色
    QPalette pal;
    pal.setColor(QPalette::Background, Qt::lightGray);
    this->setAutoFillBackground(true);
    this->setPalette(pal);

    //左上角的图标
   QPixmap icoPix(":/Image/logo.png");
   icoPix.scaled(ui->iconLabel->size(), Qt::IgnoreAspectRatio,  Qt::SmoothTransformation);
   ui->iconLabel->setScaledContents(true);
   ui->iconLabel->setPixmap(icoPix);

}

void MainInterface::initTray()
{
    //创建托盘
    this->initTrayIcon();
    connect(trayIcon, &QSystemTrayIcon::activated,
                this, &MainInterface::iconActivated);
    //托盘事件
    connect(trayShowMainAction, &QAction::triggered,
            this, &MainInterface::onShowMainAction);
     connect(trayExitAppAction, &QAction::triggered,
             this, &MainInterface::onExitAppAction);
    //this->setWindowState((this->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);

}

//界面入口
void MainInterface::initializeInterface()
{
    m_speech->say("欢迎使用xxxxx系统！"); //异步朗读文字
    this->show();
}

QWizardPage * MainInterface::createPage1(){
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("帮助对话框"));
    page->setTitle(tr("首先，非常欢迎使用本软件！让我们赶快开始吧！\n点击“选择图片”按钮，来读入将要检测的图片。"));
    page->setButtonText(QWizard::NextButton,"下一步");
    page->setButtonText(QWizard::BackButton,"上一步");
    page->setButtonText(QWizard::CancelButton,"取消");
    page->setButtonText(QWizard::FinishButton,"完成");


    QLabel *picLabel = new QLabel;
    picLabel->setPixmap(QPixmap(":/image/images/guide1.jpg"));
    QHBoxLayout *firstLayout = new QHBoxLayout;
    firstLayout->addWidget(picLabel);
    page->setLayout(firstLayout);
    return page;
}

QWizardPage * MainInterface::createPage2(){
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("第二步"));
    page->setTitle(tr("然后点击“开始检测”按钮，来开始检测工作。检测完成后，会以文字结果呈现在屏幕的右端的文字框中。"));
    page->setButtonText(QWizard::NextButton,"下一步");
    page->setButtonText(QWizard::BackButton,"上一步");
    page->setButtonText(QWizard::CancelButton,"取消");
    page->setButtonText(QWizard::FinishButton,"完成");

    QLabel *picLabel = new QLabel;//设置图片
    picLabel->setPixmap(QPixmap(":/image/images/guide2.jpg"));
    QHBoxLayout *secondLayout = new QHBoxLayout;
    secondLayout->addWidget(picLabel);
    page->setLayout(secondLayout);

    return page;
}

QWizardPage * MainInterface::createPage3(){
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("第三步"));
    page->setTitle(tr("如果需要图片的检测结果，请点击“结果展示”按钮按钮。"));
    page->setButtonText(QWizard::NextButton,"下一步");
    page->setButtonText(QWizard::BackButton,"上一步");
    page->setButtonText(QWizard::CancelButton,"取消");
    page->setButtonText(QWizard::FinishButton,"完成");

    QLabel *picLabel = new QLabel;//设置图片
    picLabel->setPixmap(QPixmap(":/image/images/guide3.jpg"));
    QHBoxLayout *thirdLayout = new QHBoxLayout;
    thirdLayout->addWidget(picLabel);
    page->setLayout(thirdLayout);
    return page;
}

QWizardPage * MainInterface::createPage4(){
    QWizardPage * page = new QWizardPage;
    page->setTitle(tr("第四步"));
    page->setTitle(tr("如果你需要保存检测后的结果图片，请点击“保存结果”按钮。"));
    page->setButtonText(QWizard::NextButton,"下一步");
    page->setButtonText(QWizard::BackButton,"上一步");
    page->setButtonText(QWizard::CancelButton,"取消");
    page->setButtonText(QWizard::FinishButton,"完成");

    QLabel *picLabel = new QLabel;//设置图片
    picLabel->setPixmap(QPixmap(":/image/images/guide4.jpg"));
    QHBoxLayout *fourthLayout = new QHBoxLayout;
    fourthLayout->addWidget(picLabel);
    page->setLayout(fourthLayout);
    return page;
}

void MainInterface::on_action_U_triggered()
{
    QWizard wizard(this);
    wizard.setOption(QWizard::NoBackButtonOnStartPage );//设置第一页没有上一步按钮
    wizard.setWizardStyle( QWizard::ModernStyle );//设置上一步下一步等按钮的显示格式
  //  wizard.setWindowTitle(tr("帮助对话框"));//设置框的标题
    wizard.addPage(createPage1());
    wizard.addPage(createPage2());
    wizard.addPage(createPage3());
    wizard.addPage(createPage4());
    wizard.exec();
}

void MainInterface::on_action_Bug_triggered()
{
       QMessageBox::about(this,tr("帮助我们做的更好！"),tr("谢谢你，帮助我们做的更好！\nbug报告网站："));
}

void MainInterface::on_action_A_triggered()
{
    QMessageBox::about(this,tr("关于本软件"),tr("这是xxx软件v1.0版本，欢迎使用！作者:xxx"));
  //Qfile file(":/detectionFile/source/SVM_HOG.xml")


}

